﻿using CoreHelper;
using CoreHelper.Mapper;
using System;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp1
{

    class Program
    {
        static void Main(string[] args)
        {
            using (var image = Image.FromFile(@"C:\Users\loogn\Desktop\a.png"))
            {
                var picSize = AdjustSize(200, 100, image.Width, image.Height);
                var thumb = image.GetThumbnailImage(picSize.Width, picSize.Height, () => false, IntPtr.Zero);
                thumb.Save(@"C:\Users\loogn\Desktop\a_s.png");
                thumb.Dispose();
            }
        }


        public static Size AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
        {
            Size size = new Size();
            // 原始宽高在指定宽高范围内，不作任何处理 
            if (orgWidth <= spcWidth && orgHeight <= spcHeight)
            {
                size.Width = orgWidth;
                size.Height = orgHeight;
            }
            else
            {
                // 取得比例系数 
                float w = orgWidth / (float)spcWidth;
                float h = orgHeight / (float)spcHeight;
                // 宽度比大于高度比 
                if (w > h)
                {
                    size.Width = spcWidth;
                    size.Height = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
                }
                // 宽度比小于高度比 
                else if (w < h)
                {
                    size.Height = spcHeight;
                    size.Width = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
                }
                // 宽度比等于高度比 
                else
                {
                    size.Width = spcWidth;
                    size.Height = spcHeight;
                }
            }
            return size;
        }

    }
}
